package com.vhukze.basics.util;

import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit;
import cn.hutool.json.JSONObject;

/**
 * @author vhukze
 * @date 2022/9/28 14:52
 */
public class CacheUtil {

    private static final TimedCache<Object, Object> CACHE = cn.hutool.cache.CacheUtil.newTimedCache(DateUnit.DAY.getMillis());

    public static void set(String key, String value) {
        CACHE.put(key, value);
    }

    public static void set(String key, String value, long timeout) {
        CACHE.put(key, value, timeout);
    }

    public static String get(String key) {
        Object obj = CACHE.get(key);
        return obj == null ? "" : obj.toString();
    }

    public static JSONObject kv() {
        JSONObject object = new JSONObject();
        CACHE.keySet().forEach(key -> object.set(key.toString(), CacheUtil.get(key.toString())));
        return object;
    }

}
